package com.lk.base.utils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class CalcUtil {

	public static boolean calcBoolean(String formula) {
		ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
		try {
			return (boolean)jse.eval(formula);
		} catch (ScriptException e) {
			e.printStackTrace();
		}
		return false;
	}
	public static String calcString(String formula,int precision) {
		ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
		String str = "";
		try {
			Object eval = jse.eval(formula);
			if(eval instanceof Integer) {
				str= String.format("%."+precision+"f",Double.valueOf((Integer)eval )) ;
			}else {
				str= String.format("%."+precision+"f",Double.valueOf((Double) eval)) ;
			}
		} catch (ScriptException e) {
			e.printStackTrace();
		}
		return str;
	}
	public static String calcString(String formula) {
		ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");
		String str = "";
		try {
			Object eval = jse.eval(formula);
			if(eval instanceof Integer) {
				str= Double.valueOf((Integer)eval ).toString() ;
			}else {
				str= Double.valueOf((Double) eval) .toString();
			}
		} catch (ScriptException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static void main(String[] args) {

	}
}
